package org.csu.secondhand.api.controller.fronted;

import org.csu.secondhand.api.entity.Buying;
import org.csu.secondhand.api.entity.Dealing;
import org.csu.secondhand.api.service.BuyingService;
import org.csu.secondhand.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/buying")
public class BuyingCotronller {
    @Autowired
    BuyingService buyingService;

    @PostMapping("insertBuying")
    @ResponseBody
    public Map<Object, Object> insertBuying(String username, long dealing) {
        Map<Object, Object> map = new HashMap<>();
        Buying buying = new Buying(0, "", TimeUtil.getTime(), username, dealing, "已提交");
        buyingService.insertBuying(buying);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buying);
        return map;
    }

    @GetMapping("getBuyingByUser")
    @ResponseBody
    public Map<Object, Object> getBuyingByUser(String username) {
        Map<Object, Object> map = new HashMap<>();
        List<Dealing> buyingList = buyingService.getBuyingByUser(username);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buyingList);
        return map;
    }

    @GetMapping("getSellingByUser")
    @ResponseBody
    public Map<Object, Object> getSellingByUser(String username) {
        Map<Object, Object> map = new HashMap<>();
        List<Dealing> buyingList = buyingService.getSellingByUser(username);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buyingList);
        return map;
    }

    @GetMapping("getBuyingList")
    @ResponseBody
    public Map<Object, Object> getBuyingStatus(String username) {
        Map<Object, Object> map = new HashMap<>();
        List<Buying> buyingList = buyingService.getBuyingList(username);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buyingList);
        return map;
    }

    @GetMapping("getSellingList")
    @ResponseBody
    public Map<Object, Object> getSellingList(String username) {
        Map<Object, Object> map = new HashMap<>();
        List<Buying> buyingList = buyingService.getSellingList(username);
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buyingList);
        return map;
    }

    @PostMapping("deleteBuying")
    @ResponseBody
    public Map<Object, Object> deleteBuying(long id) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buyingService.removeBuying(id));
        return map;
    }

    @PostMapping("changeStatus")
    @ResponseBody
    public Map<Object, Object> changeStatus(long id, String status) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buyingService.changeStatus(id, status));
        return map;
    }

    @GetMapping("getDealingByBuying")
    @ResponseBody
    public Map<Object, Object> getDealingByBuying(long id) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buyingService.getDealingByBuying(id));
        return map;
    }

    @GetMapping("getBuyingById")
    @ResponseBody
    public Map<Object, Object> getBuyingById(long id) {
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "SUCCESS");
        map.put("data", buyingService.getBuyingById(id));
        return map;
    }
}
